home *** CD-ROM | disk | FTP | other *** search
- #include <windows.h>
-
- HPALETTE BuildPalette(void);
- long FAR PASCAL WndProc (HWND hWnd, WORD iMessage, WORD wParam, LONG lParam);
- int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
- {
- static HWND hWnd;
- MSG Message;
- WNDCLASS WndClass;
-
- if (!hPrevInstance)
- {
- WndClass.cbClsExtra = 0;
- WndClass.cbWndExtra = 0;
- WndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
- WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- WndClass.hInstance = hInstance;
- WndClass.lpfnWndProc = (WNDPROC)WndProc;
- WndClass.lpszClassName = "BLUE";
- WndClass.lpszMenuName = NULL;
- WndClass.style = CS_HREDRAW | CS_VREDRAW;
-
- RegisterClass (&WndClass);
- }
-
- hWnd = CreateWindow ("BLUE", "Color Example",
- WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
- 0, CW_USEDEFAULT, 0, NULL, NULL,
- hInstance, NULL);
-
- ShowWindow (hWnd, nCmdShow);
- while (GetMessage (&Message, 0, 0, 0))
- {
- TranslateMessage(&Message);
- DispatchMessage(&Message);
- }
- return Message.wParam;
- }
-
- /************************************************/
- /* Window Procedure : WndProc */
- /************************************************/
-
- long FAR PASCAL WndProc (HWND hWnd, WORD iMessage, WORD wParam, LONG lParam)
- {
-
- //**** Device Context variable ****/
- HDC hDC;
-
- //**** Pens and brushes ****/
- LOGPEN lpBlue; // Logical outline pen.
- LOGBRUSH lbBlue; // Logical fill brush.
- static HPEN hBluePen, hOldPen; // Actual new and previous pens.
- static HBRUSH hBlueBrush, hOldBrush; // Actual new and previous brushes.
- PAINTSTRUCT PtStr; // Something used by the Begin/EndPaint routines.
-
-
- //**** General variables ****/
- short x; // Generic counter for loops.
- int curcolor, mapped; // Current Color.
-
- //**** Palette variables ****/
- static HPALETTE hPal, hOldPal; // Our palette and the previous one.
-
- switch (iMessage)
- {
- case WM_CREATE:
- hPal = BuildPalette();
- return(0);
-
-
- case WM_PAINT:
- lpBlue.lopnStyle = PS_SOLID;
- lpBlue.lopnWidth.x = 1;
- lpBlue.lopnWidth.y = 0; // Not used
-
- lbBlue.lbStyle = BS_SOLID;
- lbBlue.lbHatch = 0; // ignored when BS_SOLID
-
- hDC = BeginPaint(hWnd, &PtStr);
-
- hOldPal = SelectPalette(hDC, hPal, FALSE);
- if (!hOldPal)
- MessageBox (NULL, "Couldn't Select Palette!", "BluePal", MB_OK);
- else
- RealizePalette(hDC);
-
- for (x=1, curcolor=0; x<640; x+=16, curcolor+=6)
- {
- //*** Reference our palette indirectly:
- lpBlue.lopnColor = PALETTERGB(0,0,curcolor);
- lbBlue.lbColor = PALETTERGB(0,0,curcolor);
-
- //*** Create outline pen and fill brush:
- hBluePen = CreatePenIndirect(&lpBlue);
- hBlueBrush = CreateBrushIndirect(&lbBlue);
-
- //*** Tell hDC to use new pen&brush:
- hOldPen = SelectObject (hDC, hBluePen);
- hOldBrush = SelectObject (hDC, hBlueBrush);
-
- //*** Draw a cute picture: ***/
- RoundRect (hDC,x,100,x+16,200,10,10);
-
- //*** De-allocate current pen and brush.
- SelectObject (hDC, hOldPen);
- SelectObject (hDC, hOldBrush);
- DeleteObject(hBluePen);
- DeleteObject(hBlueBrush);
- }
-
- SelectPalette(hDC, hOldPal, FALSE); // Give back old palette.
-
-
- EndPaint(hWnd, &PtStr); // Tell GDI we're done with device context.
- return (0);
-
- case WM_PALETTECHANGED:
- if ((HWND) wParam == hWnd)
- return 0;
-
- /* Otherwise, fall through to WM_QUERYNEWPALETTE. */
-
- case WM_QUERYNEWPALETTE:
-
- /*
- * If realizing the palette causes the palette to change,
- * redraw completely.
- */
-
- hDC = GetDC(hWnd);
- hOldPal = SelectPalette (hDC, hPal, FALSE);
-
-
- mapped = RealizePalette(hDC); /* i == entries that changed */
-
- SelectPalette (hDC, hPal, FALSE);
- ReleaseDC(hWnd, hDC);
-
-
- /* If any palette entries changed, repaint the window. */
-
- if (mapped > 0)
- InvalidateRect(hWnd, NULL, TRUE);
-
- return mapped;
- case WM_DESTROY:
- DeleteObject (hPal); // Free a little memory.
- PostQuitMessage(0);
- return (0);
- default:
- return (DefWindowProc (hWnd, iMessage, wParam, lParam));
- }
- }
-
-
-
-
- HPALETTE BuildPalette(void)
- {
- /**** Palette variables ****/
- static HLOCAL hLocal; // Handle to memory allocated for logpalette.
- static NPLOGPALETTE npLogPal; // Pointer to logpalette after the memory is locked.
- int curcolor; // Current intensity of blue.
- short x; // Generic counter for loops.
- static HPALETTE hPal; // Palette handle we will return.
-
- // Allocate memory for logical palette.
- hLocal = LocalAlloc(LPTR, sizeof(LOGPALETTE) + 40 * sizeof(PALETTEENTRY));
- if (!hLocal)
- {
- MessageBox (NULL, "Couldn't Allocate Memory!", "BluePal", MB_OK);
- PostQuitMessage(0);
- return (0);
- }
- npLogPal = (NPLOGPALETTE) LocalLock(hLocal);
- npLogPal->palVersion = 0x300;
- npLogPal->palNumEntries = 40;
-
- // Fill in the colors.
- curcolor = 0;
- for (x=0, curcolor=0; x<40; x++, curcolor=curcolor+6)
- {
- npLogPal->palPalEntry[x].peBlue = (BYTE)curcolor;
- npLogPal->palPalEntry[x].peRed = 0;
- npLogPal->palPalEntry[x].peGreen = 0;
- npLogPal->palPalEntry[x].peFlags = (BYTE)0;
- }
-
- // Create the palette.
- hPal = CreatePalette((LPLOGPALETTE)npLogPal);
- if (!hPal) MessageBox (NULL, "Can't create palette!", "BluePal", MB_OK);
-
- //Free our data structure.
- LocalUnlock (hLocal);
- LocalFree (hLocal);
-
-
- return (hPal);
-
- }
-
-
-
-